// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1

CharacterBase {
    id: _root
    width: 190
    height: 100
    color: "#00dd0000"

    property int type: 0

    property real moveLenX: 5
    property real moveLenY: 0
    property int moveCount: 0

    property real toX: 10
    property real diff: 40
    property int shotInterval: 10

    property real toPX: 0
    property real toPY: 0

    Image{
        anchors.centerIn: parent
        width: 200
        height: 200
        source: "./images/Walpurgisnacht1.png"

        Rectangle{
            id: _hitEffect
            anchors.fill: parent
            color: "#00ffffff"
            ColorAnimation{
                id: _hitAmin
                target: _hitEffect
                property: "color"
                from: "#22ffffff"
                to: "#00ffffff"
                duration: 100
            }
        }
    }

    onTypeChanged: {
        switch(type){
        case 0:
            _image.source = "./images/Walpurgisnacht1.png";
            break;
        case 1:
            _image.source = "./images/Walpurgisnacht2.png";
            break;
        default:
            _image.source = "./images/Walpurgisnacht1.png";
            break;
        }
    }


    // キャラ特有の初期化処理
    function init(obj){
        shotCountMaxInFrame = 10;
        shotInterval /= ratio;
        hitpoint *= ratio;
        type = obj._type;

        move();
    }

    // 移動
    function move(){
        if(_anim.running){
        }else if(x < (fieldWidth / 2 - width / 2)){
            toPY = y + 5;
            toPX = fieldWidth * 3 / 4 - width / 2;
            _anim.start();
        }else{
            toPY = y + 5;
            toPX = fieldWidth / 4 - width / 2;
            _anim.start();
        }
    }

    ParallelAnimation {
        id: _anim
        NumberAnimation {
            target: _root;
            to: toPX
            property: "x";
            duration: 2500;
            easing.type: Easing.InOutQuad
        }
        NumberAnimation {
            target: _root;
            to: toPY
            property: "y";
            duration: 2500;
            easing.type: Easing.InOutQuad
        }
    }

    // 弾を撃つ？
    function getBullet(){
        shotCountInFrame--;
        if(shotCountInFrame < 0){
            return null;
        }else if((tickCount % shotInterval) === 0){

            toX += diff;
            if(toX >= fieldWidth){
                diff *= -1;
            }else if(toX < 0){
                diff *= -1;
            }else{
            }

            return {"_qml": "WalpurgisnachtBullet.qml"
                , "_charId": -1
                , "_x": x + width / 2
                , "_y": y
                , "_fieldWidth": fieldWidth
                , "_fieldHeight": fieldHeight
                , "_scorepoint": 100
                , "_hitpoint": 1
                , "_ratio": ratio
                , "_init":{"_toX": toX}
            };
        }else{
            return null;
        }
    }

    // 弾があたったときの処理
    onHited: {
        _hitAmin.start();
    }
}
